package com.yly.doctorapp.ui.prescription;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yly.doctorapp.R;
import com.yly.doctorapp.adapter.PrescriptionDetailItemAdapter;
import com.yly.doctorapp.adapter.PrescriptionImageAdapter;
import com.yly.doctorapp.base.BaseActivity;
import com.yly.doctorapp.base.DoctorAppApplication;
import com.yly.doctorapp.bean.ChangeBean;
import com.yly.doctorapp.bean.ImageBean;
import com.yly.doctorapp.bean.PrescriptionDetailBean;
import com.yly.doctorapp.bean.PrescriptionDetailItemBean;
import com.yly.doctorapp.bean.PrescriptionFirstBean;
import com.yly.doctorapp.bean.ProductionBean;
import com.yly.doctorapp.bean.ResponsDataBean;
import com.yly.doctorapp.constant.Configution;
import com.yly.doctorapp.listener.PrescriptionListener;
import com.yly.doctorapp.listener.PrescriptionOnclickListener;
import com.yly.doctorapp.picturebrowser.PictureBrowserActivity;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PrescriptionDetailActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_nametype)
    TextView tvNametype;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_geren)
    TextView tvGeren;
    @BindView(R.id.tv_hunyu)
    TextView tvHunyu;
    @BindView(R.id.tv_jiazu)
    TextView tvJiazu;
    @BindView(R.id.tv_jiwang)
    TextView tvJiwang;
    @BindView(R.id.tv_bingqingtezheng)
    TextView tvBingqingtezheng;
    @BindView(R.id.tv_binrenzishu)
    TextView tvBinrenzishu;
    @BindView(R.id.tv_bingqingmiaoshu)
    TextView tvBingqingmiaoshu;
    @BindView(R.id.hl_illimage)
    GridView hlIllimage;
    @BindView(R.id.hl_image)
    GridView hlImage;
    @BindView(R.id.lv_chufang)
    ListView lvChufang;
    @BindView(R.id.edit_num)
    EditText editNum;
    @BindView(R.id.edit_all)
    EditText editAll;
    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_return)
    TextView tvReturn;
    @BindView(R.id.ll_return)
    LinearLayout llReturn;
    @BindView(R.id.iv_change)
    ImageView ivChange;
    @BindView(R.id.tv_change)
    TextView tvChange;
    @BindView(R.id.ll_change)
    LinearLayout llChange;
    @BindView(R.id.iv_success)
    ImageView ivSuccess;
    @BindView(R.id.tv_success)
    TextView tvSuccess;
    @BindView(R.id.ll_success)
    LinearLayout llSuccess;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.v_image)
    View vImage;
    @BindView(R.id.tv_image)
    TextView tvImage;
    @BindView(R.id.hs_image)
    HorizontalScrollView hsImage;
    @BindView(R.id.v_illimage)
    View vIllimage;
    @BindView(R.id.tv_illimage)
    TextView tvIllimage;
    @BindView(R.id.hs_illimage)
    HorizontalScrollView hsIllimage;
    private String status;
    private String anagraphId;
    public static List<PrescriptionDetailItemBean> mPrescriptionDetailItemBeanList = new ArrayList<>();
    private PrescriptionDetailItemAdapter adapter;
    private int mPosition = 0;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_prescriptiondetail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //第一步更新按钮
        status = getIntent().getStringExtra("PRESCRIPTIONTYPE");//处方状态
        editNum.setText(getIntent().getStringExtra("INSTRUCTIONSFORTAKING"));
        editAll.setText(getIntent().getStringExtra("DOSAGE"));
        editNum.setEnabled(false);
        editAll.setEnabled(false);
        editAll.setFocusable(false);
        editAll.setFocusableInTouchMode(false);
        editNum.setFocusable(false);
        editNum.setFocusableInTouchMode(false);
        editAll.setBackground(null);
        editNum.setBackground(null);
        if (status.equals("0")) {
            llReturn.setVisibility(View.VISIBLE);
            llChange.setVisibility(View.VISIBLE);
            llSuccess.setVisibility(View.VISIBLE);
            tvSuccess.setText("通过审核");
            changeButton(2);
        } else {
            changeButton(2);
            llReturn.setVisibility(View.GONE);
            llChange.setVisibility(View.GONE);
            llSuccess.setVisibility(View.VISIBLE);
            if (status.equals("1")) {
                tvSuccess.setText("已审核");
            } else {
                tvSuccess.setText("已驳回");
            }
        }
        adapter = new PrescriptionDetailItemAdapter(PrescriptionDetailActivity.this);
        adapter.setCanEdit(false);
        lvChufang.setAdapter(adapter);
        adapter.setmListener(new PrescriptionListener() {
            @Override
            public void callBack() {
                adapter.setCanEdit(true);
                adapter.notifyDataSetChanged();
            }
        });
        adapter.setmOnclickListener(new PrescriptionOnclickListener() {
            @Override
            public void callBack(int isNum, int position) {
                mPosition = position;
                if (isNum != 0) {
                    adapter.setNum(isNum);
                    adapter.notifyDataSetChanged();
                } else {
                    Intent mIntent = new Intent(PrescriptionDetailActivity.this, SerchActivity.class);
                    startActivityForResult(mIntent, 1);
                }
            }
        });
    }

    private void initGetDetailData(String doctorId, String anagraphId) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.QUERYDETAIL);
        params.addBodyParameter("doctorid", doctorId);
        params.addBodyParameter("anagraphId", anagraphId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)) {
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")) {
                        if (!StringUtils.isEmpty(responsDataBean.getData())) {
                            List<PrescriptionDetailBean> prescriptionDetailList = JSON.parseArray(responsDataBean.getData(), PrescriptionDetailBean.class);
                            showPatientUi(prescriptionDetailList.get(0));
                            showImageUi(prescriptionDetailList);
                        }
                    } else {
                        ToastUtils.show(getApplicationContext(), responsDataBean.getMsg(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接失败", 100);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void showImageUi(List<PrescriptionDetailBean> prescriptionDetailList) {
        if (null != prescriptionDetailList && prescriptionDetailList.size() > 0) {
            //这里是病例
            if (!StringUtils.isEmpty(prescriptionDetailList.get(0).getIllPicList())) {
                tvImage.setVisibility(View.VISIBLE);
                vImage.setVisibility(View.VISIBLE);
                hsImage.setVisibility(View.VISIBLE);
                final List<ImageBean> mImageList = JSON.parseArray(prescriptionDetailList.get(0).getIllPicList(), ImageBean.class);
                if (null != mImageList && mImageList.size() > 0){
                    tvImage.setVisibility(View.VISIBLE);
                    vImage.setVisibility(View.VISIBLE);
                    hsImage.setVisibility(View.VISIBLE);
                    setGridView(hlIllimage, mImageList);
                    hlIllimage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent mIntent = new Intent(PrescriptionDetailActivity.this, PictureBrowserActivity.class);
                            mIntent.putStringArrayListExtra("FILELISTURL", getImageList(mImageList));
                            mIntent.putExtra("INDEX", position);
                            startActivity(mIntent);
                        }
                    });
                }else {
                    tvImage.setVisibility(View.GONE);
                    vImage.setVisibility(View.GONE);
                    hsImage.setVisibility(View.GONE);
                }
            }else {
                tvImage.setVisibility(View.GONE);
                vImage.setVisibility(View.GONE);
                hsImage.setVisibility(View.GONE);
            }
            //处方图
            if (!StringUtils.isEmpty(prescriptionDetailList.get(0).getAngraphPicList())) {
                vIllimage.setVisibility(View.VISIBLE);
                tvIllimage.setVisibility(View.VISIBLE);
                hsIllimage.setVisibility(View.VISIBLE);
                final List<ImageBean> mImageLists = JSON.parseArray(prescriptionDetailList.get(0).getAngraphPicList(), ImageBean.class);
                if (null != mImageLists && mImageLists.size() > 0){
                    vIllimage.setVisibility(View.VISIBLE);
                    tvIllimage.setVisibility(View.VISIBLE);
                    hsIllimage.setVisibility(View.VISIBLE);
                    setGridView(hlImage, mImageLists);
                    hlImage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent mIntent = new Intent(PrescriptionDetailActivity.this, PictureBrowserActivity.class);
                            //得到图片连接地址
                            mIntent.putStringArrayListExtra("FILELISTURL", getImageList(mImageLists));
                            mIntent.putExtra("INDEX", position);
                            startActivity(mIntent);
                        }
                    });
                }else {
                    vIllimage.setVisibility(View.GONE);
                    tvIllimage.setVisibility(View.GONE);
                    hsIllimage.setVisibility(View.GONE);
                }
            }else {
                vIllimage.setVisibility(View.GONE);
                tvIllimage.setVisibility(View.GONE);
                hsIllimage.setVisibility(View.GONE);
            }
        }
    }

    private ArrayList<String> getImageList(List<ImageBean> mImageList) {
        ArrayList<String> mlist = new ArrayList<>();
        if (null != mImageList && mImageList.size() > 0) {
            for (int i = 0; i < mImageList.size(); i++) {
                mlist.add(mImageList.get(i).getImageUrl());
            }
            return mlist;
        } else {
            return null;
        }
    }

    /**
     * 设置GirdView参数，绑定数据
     */
    private void setGridView(GridView gridView, List<ImageBean> mImageList) {
        int size = mImageList.size();
        int length = 100;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        gridView.setColumnWidth(itemWidth); // 设置列表项宽
        gridView.setHorizontalSpacing(5); // 设置列表项水平间距
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(size); // 设置列数量=列表集合数

        PrescriptionImageAdapter adapter = new PrescriptionImageAdapter(PrescriptionDetailActivity.this,
                mImageList);
        gridView.setAdapter(adapter);
    }

    private void showPatientUi(PrescriptionDetailBean prescriptionDetailBean) {
        tvName.setText(prescriptionDetailBean.getName());
        tvAge.setText(prescriptionDetailBean.getAge() + "");
        tvHunyu.setText(prescriptionDetailBean.getMarriageAndChildbearing());
        tvGeren.setText(prescriptionDetailBean.getOneSelfHistory());
        tvJiazu.setText(prescriptionDetailBean.getFamilyHistory());
        tvJiwang.setText(prescriptionDetailBean.getIllHistory());
        tvBingqingtezheng.setText(prescriptionDetailBean.getIllTag());
        tvBingqingmiaoshu.setText(prescriptionDetailBean.getIllDesc());
        tvBinrenzishu.setText(prescriptionDetailBean.getIllDescOneself());
        tvNametype.setText(prescriptionDetailBean.getIllRepresentor());
    }

    @Override
    protected void initData() {
        String doctorId = "";
        if (null != DoctorAppApplication.mUserBean) {
            doctorId = DoctorAppApplication.mUserBean.getDoctorid() + "";
        }
        anagraphId = getIntent().getStringExtra("ANAGRAPHID");
        initGetDetailData(doctorId, anagraphId);
        initQueryItem(doctorId, anagraphId);
        editNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editNum.setFocusable(true);
                editNum.setFocusableInTouchMode(true);
                editAll.setFocusableInTouchMode(false);
                editAll.setFocusable(false);
            }
        });
        editAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editAll.setFocusable(true);
                editAll.setFocusableInTouchMode(true);
                editNum.setFocusableInTouchMode(false);
                editNum.setFocusable(false);
            }
        });
    }

    private void initQueryItem(String doctorId, String anagraphId) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.QUERYPRESCRIPTIONITEM);
        params.addBodyParameter("anagraphId", anagraphId);
        params.addBodyParameter("doctorid", doctorId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mPrescriptionDetailItemBeanList.clear();
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)) {
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")) {
                        if (!StringUtils.isEmpty(responsDataBean.getData())) {
                            PrescriptionFirstBean prescriptionFirstBean = JSON.parseObject(responsDataBean.getData(), PrescriptionFirstBean.class);
                            if (!StringUtils.isEmpty(prescriptionFirstBean.getList())) {
                                List<PrescriptionDetailItemBean> prescriptionDetailItemBeanList = JSON.parseArray(prescriptionFirstBean.getList(), PrescriptionDetailItemBean.class);
                                mPrescriptionDetailItemBeanList.addAll(prescriptionDetailItemBeanList);
                                adapter.setCanEdit(false);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接失败", 100);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void changeButton(int index) {
        switch (index) {
            case 0:
                llReturn.setBackgroundColor(0xffa0524b);
                tvReturn.setTextColor(0xffffffff);
                ivReturn.setImageResource(R.mipmap.backselected);
                llChange.setBackgroundColor(0xffffffff);
                tvChange.setTextColor(0xff666666);
                ivChange.setImageResource(R.mipmap.change);
                llSuccess.setBackgroundColor(0xffffffff);
                tvSuccess.setTextColor(0xff666666);
                ivSuccess.setImageResource(R.mipmap.tongguo);
                break;
            case 1:
                llReturn.setBackgroundColor(0xffffffff);
                tvReturn.setTextColor(0xff666666);
                ivReturn.setImageResource(R.mipmap.doctorback);
                llChange.setBackgroundColor(0xffa0524b);
                tvChange.setTextColor(0xffffffff);
                ivChange.setImageResource(R.mipmap.changeselect);
                llSuccess.setBackgroundColor(0xffffffff);
                tvSuccess.setTextColor(0xff666666);
                ivSuccess.setImageResource(R.mipmap.tongguo);
                break;
            case 2:
                llReturn.setBackgroundColor(0xffffffff);
                tvReturn.setTextColor(0xff666666);
                ivReturn.setImageResource(R.mipmap.doctorback);
                llChange.setBackgroundColor(0xffffffff);
                tvChange.setTextColor(0xff666666);
                ivChange.setImageResource(R.mipmap.change);
                llSuccess.setBackgroundColor(0xffa0524b);
                tvSuccess.setTextColor(0xffffffff);
                ivSuccess.setImageResource(R.mipmap.tongguoselect);
                break;
        }
    }

    private void initUpdatePrescription(final int status, final String rejectReason) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.UPDATEPRESCRIPTION);
        params.addBodyParameter("anagraphId", anagraphId);
        if (status == 2) {
            params.addBodyParameter("status", "2");
            params.addBodyParameter("rejectReason", rejectReason);
        } else {
            params.addBodyParameter("status", status + "");
            params.addBodyParameter("info", editNum.getText().toString());
            params.addBodyParameter("dosage", editAll.getText().toString());
            //给集合
            inserList(params);
        }
        params.addHeader("Content-Types", "application/json");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)) {
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")) {
                        if (status != 0){
                            finish();
                        }
                    }
                    ToastUtils.show(getApplicationContext(), responsDataBean.getMessage(), 100);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接失败", 100);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void inserList(RequestParams params) {
        List<ChangeBean> changeBeanList = new ArrayList<>();
        if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0) {
            for (int i = 0; i < mPrescriptionDetailItemBeanList.size(); i++) {
                ChangeBean changeBean = new ChangeBean();
                changeBean.setDosage(mPrescriptionDetailItemBeanList.get(i).getDosage() + "");
                changeBean.setDiscountPrice(mPrescriptionDetailItemBeanList.get(i).getDiscountPrice());
                changeBean.setIsSecret(mPrescriptionDetailItemBeanList.get(i).getIsSecret() + "");
                changeBean.setOriginalPrice(mPrescriptionDetailItemBeanList.get(i).getOriginalPrice());
                changeBean.setProductId(mPrescriptionDetailItemBeanList.get(i).getProductId());
                changeBean.setNum(mPrescriptionDetailItemBeanList.get(i).getNum());
                changeBean.setUnit(mPrescriptionDetailItemBeanList.get(i).getUnit());
                changeBeanList.add(changeBean);
            }
        }
        params.addBodyParameter("productArr", JSON.toJSONString(changeBeanList));
    }

    @OnClick({R.id.iv_back, R.id.ll_return, R.id.ll_change, R.id.ll_success})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_return:
                if (status.equals("0")) {
                    //这里弹出写理由的框
                    showReturnDialog();
                    changeButton(0);
                }
                break;
            case R.id.ll_change:
                if (status.equals("0")) {
                    //这里不需要调接口  直接本地操作  变成可以编辑的状态
                    if (tvChange.getText().toString().equals("修改")){
                        tvChange.setText("完成修改");
                        adapter.setCanEdit(true);
                        adapter.setNum(0);
                        adapter.notifyDataSetChanged();
                        //添加足view
                        addFootView();
//                        llChange.setEnabled(false);
                        editNum.setEnabled(true);
                        editAll.setEnabled(true);
                        editNum.setBackgroundResource(R.drawable.line_returnbg);
                        editAll.setBackgroundResource(R.drawable.line_returnbg);
                    }else {
                        if (!StringUtils.isEmpty(edit_yao.getText().toString()) && !StringUtils.isEmpty(edit_num.getText().toString())){
                            saveData();
                        }
                        tvChange.setText("修改");
                        adapter.setCanEdit(false);
                        adapter.notifyDataSetChanged();
                        removeFootView();
                        editNum.setEnabled(false);
                        editAll.setEnabled(false);
                        editNum.setBackground(null);
                        editAll.setBackground(null);
                        if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0) {
                            initUpdatePrescription(0, "");
                        }else {
                            ToastUtils.show(getApplicationContext(), "请添加相应的产品", 100);
                        }
                    }
                    changeButton(1);
                }
                break;
            case R.id.ll_success:
                if (status.equals("0")) {
//                    mPrescriptionDetailItemBeanList.clear();
                    adapter.setCanEdit(false);
                    removeFootView();
                    adapter.notifyDataSetChanged();
                    if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0) {
                        initUpdatePrescription(1, "");
                    }else {
                        ToastUtils.show(getApplicationContext(), "请添加相应的产品", 100);
                    }
                    changeButton(2);
                }
                break;
        }
    }

    private void saveData() {
        if (null != mProduction){
            if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0) {
                if (mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).getProductId() == 0) {
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setName(mProduction.getName());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setProductId(mProduction.getProductId());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setIsSecret(mProduction.getIsSecret());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setDiscountPrice(mProduction.getDiscountPrice());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setOriginalPrice(mProduction.getOriginalPrice());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setNum(edit_num.getText().toString());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setName(mProduction.getName());
                    mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setUnit(edit_danwei.getText().toString().replace("单位:", ""));
                } else {
                    PrescriptionDetailItemBean prescriptionDetailItemBean = new PrescriptionDetailItemBean();
                    prescriptionDetailItemBean.setName(mProduction.getName());
                    prescriptionDetailItemBean.setProductId(mProduction.getProductId());
                    prescriptionDetailItemBean.setIsSecret(mProduction.getIsSecret());
                    prescriptionDetailItemBean.setDiscountPrice(mProduction.getDiscountPrice());
                    prescriptionDetailItemBean.setOriginalPrice(mProduction.getOriginalPrice());
                    prescriptionDetailItemBean.setNum(edit_num.getText().toString());
                    prescriptionDetailItemBean.setName(mProduction.getName());
                    prescriptionDetailItemBean.setUnit(edit_danwei.getText().toString().replace("单位:", ""));
                    mPrescriptionDetailItemBeanList.add(prescriptionDetailItemBean);
                }
            } else {
                PrescriptionDetailItemBean prescriptionDetailItemBean = new PrescriptionDetailItemBean();
                prescriptionDetailItemBean.setName(mProduction.getName());
                prescriptionDetailItemBean.setProductId(mProduction.getProductId());
                prescriptionDetailItemBean.setIsSecret(mProduction.getIsSecret());
                prescriptionDetailItemBean.setDiscountPrice(mProduction.getDiscountPrice());
                prescriptionDetailItemBean.setOriginalPrice(mProduction.getOriginalPrice());
                prescriptionDetailItemBean.setNum(edit_num.getText().toString());
                prescriptionDetailItemBean.setName(mProduction.getName());
                prescriptionDetailItemBean.setUnit(edit_danwei.getText().toString().replace("单位:", ""));
                mPrescriptionDetailItemBeanList.add(prescriptionDetailItemBean);
            }
        }
    }

    private View footView;
    private EditText edit_yao;
    private EditText edit_num;
    private EditText edit_danwei;
    private void addFootView() {
        footView = View.inflate(PrescriptionDetailActivity.this, R.layout.layout_ppdetailifoot, null);
        edit_yao = footView.findViewById(R.id.edit_yao);
        edit_num = footView.findViewById(R.id.edit_num);
        edit_danwei = footView.findViewById(R.id.edit_danwei);
        edit_yao.setEnabled(true);
        edit_num.setEnabled(true);
        edit_danwei.setEnabled(false);
        edit_yao.setFocusable(false);
        edit_yao.setFocusableInTouchMode(false);
        edit_num.setFocusable(false);
        edit_num.setFocusableInTouchMode(false);
        edit_danwei.setFocusable(false);
        edit_danwei.setFocusableInTouchMode(false);
        edit_yao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(PrescriptionDetailActivity.this, SerchActivity.class);
                startActivityForResult(mIntent, 0);
            }
        });
        edit_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_yao.setFocusable(false);
                edit_yao.setFocusableInTouchMode(false);
                edit_num.setFocusable(true);
                edit_num.setFocusableInTouchMode(true);
                edit_danwei.setFocusable(false);
                edit_danwei.setFocusableInTouchMode(false);
            }
        });
        edit_danwei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_yao.setFocusable(false);
                edit_yao.setFocusableInTouchMode(false);
                edit_num.setFocusable(false);
                edit_num.setFocusableInTouchMode(false);
                edit_danwei.setFocusable(false);
                edit_danwei.setFocusableInTouchMode(false);
            }
        });
        ImageView iv_add = footView.findViewById(R.id.iv_add);
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(edit_yao.getText().toString()) && !StringUtils.isEmpty(edit_num.getText().toString()) && !StringUtils.isEmpty(edit_danwei.getText().toString())) {
                    //把对应的信息添加到集合去
                    if (null != mProduction){
                        saveData();
                    }
//                    if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0) {
//                        mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setNum(edit_num.getText().toString());
//                        mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setName(edit_yao.getText().toString());
//                        mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setUnit(edit_danwei.getText().toString().replace("单位:", ""));
//                    } else {
//                        PrescriptionDetailItemBean prescriptionDetailItemBean = new PrescriptionDetailItemBean();
//                        prescriptionDetailItemBean.setName(edit_yao.getText().toString());
//                        prescriptionDetailItemBean.setNum(edit_num.getText().toString());
//                        prescriptionDetailItemBean.setUnit(edit_danwei.getText().toString().replace("单位:", ""));
//                        mPrescriptionDetailItemBeanList.add(prescriptionDetailItemBean);
//                    }
                    adapter.setCanEdit(true);
                    adapter.notifyDataSetChanged();
                    removeFootView();
                    addFootView();
                } else {
                    ToastUtils.show(PrescriptionDetailActivity.this, "请填写", 100);
                }
            }
        });
        lvChufang.addFooterView(footView);
    }

    private ProductionBean mProduction;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            mProduction = (ProductionBean) data.getSerializableExtra("production");
            if (requestCode == 0) {
                //判重
                if (!isHave(mProduction)){
                    edit_yao.setText(mProduction.getName());
                    if (!StringUtils.isEmpty(edit_num.getText().toString())){
                        if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0) {
                            if (mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).getProductId() == 0) {
                                mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setName(mProduction.getName());
                                mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setProductId(mProduction.getProductId());
                                mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setIsSecret(mProduction.getIsSecret());
                                mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setDiscountPrice(mProduction.getDiscountPrice());
                                mPrescriptionDetailItemBeanList.get((mPrescriptionDetailItemBeanList.size() - 1)).setOriginalPrice(mProduction.getOriginalPrice());
                            } else {
                                PrescriptionDetailItemBean prescriptionDetailItemBean = new PrescriptionDetailItemBean();
                                prescriptionDetailItemBean.setName(mProduction.getName());
                                prescriptionDetailItemBean.setProductId(mProduction.getProductId());
                                prescriptionDetailItemBean.setIsSecret(mProduction.getIsSecret());
                                prescriptionDetailItemBean.setDiscountPrice(mProduction.getDiscountPrice());
                                prescriptionDetailItemBean.setOriginalPrice(mProduction.getOriginalPrice());
                                mPrescriptionDetailItemBeanList.add(prescriptionDetailItemBean);
                            }
                        } else {
                            PrescriptionDetailItemBean prescriptionDetailItemBean = new PrescriptionDetailItemBean();
                            prescriptionDetailItemBean.setName(mProduction.getName());
                            prescriptionDetailItemBean.setProductId(mProduction.getProductId());
                            prescriptionDetailItemBean.setIsSecret(mProduction.getIsSecret());
                            prescriptionDetailItemBean.setDiscountPrice(mProduction.getDiscountPrice());
                            prescriptionDetailItemBean.setOriginalPrice(mProduction.getOriginalPrice());
                            mPrescriptionDetailItemBeanList.add(prescriptionDetailItemBean);
                        }
                    }
                }else {
                    ToastUtils.show(getApplicationContext(), "请不要添加相同的产品", 100);
                }
            } else {
                mPrescriptionDetailItemBeanList.get(mPosition).setName(mProduction.getName());
                mPrescriptionDetailItemBeanList.get(mPosition).setProductId(mProduction.getProductId());
                adapter.setCanEdit(true);
                adapter.notifyDataSetChanged();
            }
        }
    }

    private boolean isHave(ProductionBean mProduction) {
        boolean isHaveData = false;
        if (null != mPrescriptionDetailItemBeanList && mPrescriptionDetailItemBeanList.size() > 0){
            for (int i = 0; i < mPrescriptionDetailItemBeanList.size(); i++){
                if (mPrescriptionDetailItemBeanList.get(i).getProductId() == mProduction.getProductId()){
                    isHaveData = true;
                }
            }
        }
        return isHaveData;
    }

    private void removeFootView() {
        if (null != footView) {
            lvChufang.removeFooterView(footView);
        }
    }

    private void showReturnDialog() {
        final Dialog dialog = new Dialog(PrescriptionDetailActivity.this, R.style.buscarddialog);
        View view = View.inflate(PrescriptionDetailActivity.this, R.layout.layout_returndialog, null);
        dialog.setContentView(view);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                changeButton(2);
            }
        });
        TextView tv_sure = view.findViewById(R.id.tv_sure);
        final EditText edit_return = view.findViewById(R.id.edit_return);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(edit_return.getText().toString())) {
                    ToastUtils.show(PrescriptionDetailActivity.this, "请输入驳回理由", 100);
                    return;
                }
                initUpdatePrescription(2, edit_return.getText().toString());
                dialog.dismiss();
            }
        });
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DeviceUtils.deviceWidth(mContext) * 0.5);
        lp.height = (int) (DeviceUtils.deviceWidth(mContext) * 0.4);
        dialogWindow.setAttributes(lp);
    }
}
